#!/usr/bin/perl -w
use strict;
use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin");


my @files = @ARGV;
@files = sort @files;
my @results;
my %species;
for (@files)
{
	next unless -f $_;
	open FILE, $_ or die "Error:\n\tCould not open $_";
	die "File $_ not matching " unless 
	s/^.*\d+\.(.*).gene_counts.summary$/$1/;
	push(@results, {name => $_});
	while (<FILE>)
	{
		if (/(\d+)\s+used genes in total/)
		{
			$results[-1]{total} = $1;
		}
		elsif (/(\d+)\s+used genes from species\s+(.*)/)
		{
			$species{$2}++;
			$results[-1]{$2} = $1;
		}
	}
	die "No results in $_ " unless keys %{$results[-1]};
}
delete $species{Unknown};
my @species = sort keys %species;
print "Stage\tTotal\t", join ("\t",  @species),  "\n";
for (@results)
{
	print "$_->{name}";
	print "\t$_->{total}";
	for my $species (@species)
	{
		if (exists $_->{$species})
		{
			print "\t$_->{$species}";
		}
		else
		{
			print "\t";
		}
	}
	print "\n";
}
